﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Globalization;

namespace DTC.Converters
{
    [ValueConversion(typeof(TimeSpan), typeof(string))]
    public class TimeSpanToStringConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
            CultureInfo culture)
        {
            return ConvertTo(value, targetType);
        }
        public object ConvertBack(object value, Type targetType, object parameter,
            CultureInfo culture)
        {
            return ConvertFrom(value, targetType);
        }
        public object ConvertTo(object value, Type targetType)
        {
            if (value == null)
                return "";
            TimeSpan ts = (TimeSpan)value;
            string result = "";
            if (ts.TotalDays != 0)
            {
                if (ts.TotalDays >= 30)
                {
                    //treat like months
                    int MounthCount = (int)Math.Floor(ts.TotalDays / 30);
                    if (MounthCount == 1)
                    {
                        result += "1 месяц";
                    }
                    else if (MounthCount >= 2 && MounthCount <= 4)
                    {
                        result += MounthCount.ToString() + " месяца";
                    }
                    else result += MounthCount.ToString() + " месяцев";


                }
                else if (ts.TotalDays >= 7 && ts.TotalDays < 30)
                {
                    int WeekCount = (int)Math.Floor(ts.TotalDays / 7);
                    if (WeekCount == 1)
                    {
                        result += "1 неделю";
                    }
                    else if (WeekCount >= 2 && WeekCount <= 4)
                    {
                        result += WeekCount.ToString() + " недели";
                    }
                    else result += WeekCount.ToString() + " недель";
                }
                else
                {
                    if (ts.TotalDays == 1)
                    {
                        result += "1 день";
                    }
                    else if (ts.TotalDays >= 2 && ts.TotalDays <= 4)
                    {
                        result += ts.TotalDays.ToString() + " дня";
                    }
                    else result += ts.TotalDays.ToString() + " дней";
                }
            }
            return result;
        }
        public object ConvertFrom(object value, Type targetType)
        {
            string str = (string)value;
            string ValuePart="";
            string ValueSizePart="";
            int LastDigitIndex = 0;
            int TimeVal = 0;
            int Multiplier=1;
            for (int j = 0; j < str.Length; j++)
            {
                if (char.IsDigit(str[j]))
                {
                    ValuePart += str[j];
                    LastDigitIndex++;
                }
                if (char.IsLetter(str[j]))
                {
                    if (j < LastDigitIndex)
                        throw new Exception();
                    ValueSizePart += str[j];
                }
            }
            try
            {
                int.TryParse(ValuePart, out TimeVal);
            }
            catch (Exception e)
            {
                throw e;
            }
            ValueSizePart = ValueSizePart.ToLower();
            if (ValueSizePart.StartsWith("мес"))
                Multiplier = 30;
            else if (ValueSizePart.StartsWith("нед"))
                Multiplier = 7;
            return new TimeSpan(Multiplier * TimeVal, 0, 0, 0);
        }
    }
}
